package com.axter.libs.photo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.Toast;

import java.io.File;

/**
 * Created by zhaobo on 2016/10/14.
 */

public class BaseUtils {
    public static boolean openCamera(Activity activity, String picPath, int request_code) {
        if (activity == null)
            return false;
        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(picPath)));
        it.putExtra(MediaStore.Images.Media.ORIENTATION, 0);

        if (it.resolveActivity(activity.getPackageManager()) != null) {
            activity.startActivityForResult(it, request_code);
            return true;
        } else {
            Toast.makeText(activity, "sorry,i can't load camera", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    public static String getFilePath(Context appContext, String dirPath, String fileName) {
        // 生成视频缩略图
        if (TextUtils.isEmpty(dirPath)) {
            dirPath = appContext.getCacheDir().getAbsolutePath();
        }
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            if (!dirFile.mkdirs()) {
                dirFile = appContext.getCacheDir();
            }
        }
        return new File(dirFile, fileName).getAbsolutePath();
    }
}
